package com.googlecode.puddle;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.googlecode.puddle.data.PuddleDbAdapter;

public class ViewPodcast extends Activity {

	private static final String TAG = ViewPodcast.class.getName();

    private static final int MENU_DELETE_PODCAST = 0;
    
	private static final int ACTIVITY_VIEW_SHOW = 0;
    private static final int ACTIVITY_DELETE_PODCAST = 1;

	private static final int DIALOG_DELETE_PODCAST = 0;
	
	private long podcast = -1;
	private PuddleDbAdapter podcastDb;
	
	private TextView titleTextView;
	private TextView descriptionTextView;
	private ListView showList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.view_podcast);
		
		showList = (ListView) findViewById(R.id.view_podcast_list_shows);
		titleTextView = (TextView) findViewById(R.id.view_podcast_title);
		descriptionTextView = (TextView) findViewById(R.id.view_podcast_description);
		
		podcastDb = new PuddleDbAdapter(this);
		podcastDb.open();
		
		Intent intent = getIntent();
		podcast = intent.getLongExtra("podcast", -1);
		
		fillData();
		
		showList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
				viewShow(id);
			}
		});
	}

	protected void viewShow(long id) {
    	Intent i = new Intent(this, ViewShow.class);
    	i.putExtra("show", id);
    	startActivityForResult(i, ACTIVITY_VIEW_SHOW);
	}
	
	private void fillData() {
		Cursor c = podcastDb.fetchPodcast(podcast);
		String title = c.getString(c.getColumnIndex(PuddleDbAdapter.PODCAST_TITLE));
		titleTextView.setText(title);
		String description = c.getString(c.getColumnIndex(PuddleDbAdapter.PODCAST_DESCRIPTION));
		descriptionTextView.setText(description);
		c.close();
		
		Cursor showsCursor = podcastDb.fetchAllShowsForPodcast(podcast);
		startManagingCursor(showsCursor);

		String[] from = new String[] { PuddleDbAdapter.SHOW_TITLE, PuddleDbAdapter.SHOW_DATE };
		int[] to = new int[] { R.id.list_show_row_title, R.id.list_show_row_date };

		SimpleCursorAdapter showsAdapter = new SimpleCursorAdapter(this, R.layout.list_show_row, showsCursor,
				from, to);
		
		showList.setAdapter(showsAdapter);
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        Log.d(TAG, "Creating options menu");
        menu.add(0, MENU_DELETE_PODCAST, 0, "Delete Podcast");
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        int itemId = item.getItemId();
		Log.d(TAG, "Menu item selected: " + itemId);
        switch(itemId) {
        case MENU_DELETE_PODCAST:
        	showDialog(DIALOG_DELETE_PODCAST);
        	return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
	protected Dialog onCreateDialog(int id) {
    	Dialog dialog = null;
		switch (id) {
		case DIALOG_DELETE_PODCAST:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setMessage("Are you sure you want to delete this podcast?")
	    	       .setCancelable(false)
	    	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	        	   podcastDb.deletePodcast(podcast);
	    	        	   finish();
	    	           }
	    	       })
	    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                dialog.cancel();
	    	           }
	    	       });
	    	dialog = builder.create();
			break;
		}
		return dialog;
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        
        if (requestCode == ACTIVITY_DELETE_PODCAST && RESULT_OK == resultCode) {
        	finish();
        	return;
        }
        
        fillData();
    }
}